<template>
  <div class="category">
    <h3>图书分类</h3>
    <div v-for="category in categories" :key="category.id">
      <h5>{{ category.name }}</h5>
      <router-link
        v-for="child in category.children"
        :key="child.id"
        :to="'/category/' + child.id"
      >
        {{ child.name }}
      </router-link>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { getCategory } from '@/api/api'
interface Icategories {
  id: number
  name: string
  parentId: number | null
  root: boolean
  children: Icategories[] | null
}
const categories = ref<Icategories[]>([])
const getCategories = async () => {
  const { data } = await getCategory()
  // console.log(data)
  categories.value = data
  console.log(categories)
}
getCategories()
</script>

<style scoped>
.category {
  margin-left: 60px;
  margin-top: 10px;
  float: left;
  border: solid 1px #ccc;
  width: 15%;
}
.category div {
  margin-bottom: 10px;
}
.category a {
  padding: 0 5px 0 5px;
}
</style>
